package com.ruoyi.classai.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.classai.domain.EwcChapterTeachingResources;
import com.ruoyi.classai.domain.vo.LearningAnalytics.AnalyticsStuConditionVo;
import com.ruoyi.classai.domain.vo.LearningAnalytics.AnalyticsStuCountVo;
import com.ruoyi.classai.domain.vo.LearningAnalytics.AnalyticsStuScoreVo;
import com.ruoyi.classai.mapper.LearningStatisticsMapper;
import com.ruoyi.classai.service.LearningStatisticsService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
public class LearningStatisticsServiceImpl implements LearningStatisticsService {
    @Autowired
    private LearningStatisticsMapper learningStatisticsMapper;


    @Override
    public AjaxResult getLearningStatisticsInfo(String courseId, String chapterId, String classId) {
        //及格分数线
        Integer passScore = 60;
        //优秀分数线
        Integer excellentScore = 80;
        //问题达标分数线
        Integer reachScore = 80;
        //查询完成作业批改标识
        String correctStatus = "1";
        //模块标识
        String moduleCode = "04001";
        //高分分数线
        int highScore = 0;
        //查需关注分数线
        int lowScore = 0;
        JSONObject resultJson = new JSONObject();
        JSONObject analyticsScoreJson = new JSONObject();
        JSONObject unReachCountJson=new JSONObject();

        //查学生人数
        AnalyticsStuCountVo analyticsStuCountVo = learningStatisticsMapper.getAnalyticsStuCount(courseId,chapterId,classId);
        //查学生分数
        AnalyticsStuScoreVo analyticsStuScoreVo = learningStatisticsMapper.
                getAnalyticsStuScore(courseId,chapterId,classId,passScore,excellentScore,reachScore,correctStatus,moduleCode);
        if(Objects.nonNull(analyticsStuScoreVo)){
            analyticsScoreJson.put("failCount",analyticsStuScoreVo.getFailCount());//不及格
            analyticsScoreJson.put("passCount",analyticsStuScoreVo.getPassCount());//及格
            analyticsScoreJson.put("excellentCount",analyticsStuScoreVo.getExcellentCount());//优秀
            unReachCountJson.put("synthesisSpeedFailCount",analyticsStuScoreVo.getSynthesisSpeedFailCount());//速度不达标
            unReachCountJson.put("synthesisGestureFailCount",analyticsStuScoreVo.getSynthesisGestureFailCount());//姿态不标准
            unReachCountJson.put("synthesisRhythmExcellentFailCount",analyticsStuScoreVo.getSynthesisRhythmExcellentFailCount());//节奏不稳定
        }
        //查高分学生
        highScore = 90;
        List<AnalyticsStuConditionVo> analyticsStuConditionHighList = learningStatisticsMapper.
                getAnalyticsStuCondition(courseId,classId,highScore,lowScore);
        //查需关注学生
        highScore = 0;
        lowScore = 70;
        List<AnalyticsStuConditionVo> analyticsStuConditionLowList = learningStatisticsMapper.
                getAnalyticsStuCondition(courseId,classId,highScore,lowScore);
        //查AI建议
        EwcChapterTeachingResources chapterTeachingResources = learningStatisticsMapper.getAnalyticsAISuggestion(courseId,classId,chapterId);
        if(Objects.nonNull(chapterTeachingResources)){
            resultJson.put("studySuggestion",chapterTeachingResources.getStudySuggestion());
            resultJson.put("groupSuggestion",chapterTeachingResources.getGroupSuggestion());
        }
        resultJson.put("analyticsStuCountVo", analyticsStuCountVo);
        resultJson.put("analyticsStuScoreVo", analyticsStuScoreVo);
        resultJson.put("analyticsScoreJson", analyticsScoreJson);
        resultJson.put("unReachCountJson", unReachCountJson);
        resultJson.put("analyticsStuConditionHighList", analyticsStuConditionHighList);
        resultJson.put("analyticsStuConditionLowList", analyticsStuConditionLowList);
        return AjaxResult.success("查询成功", resultJson);
    }
}
